package com.ruoyi.project.business.schedulerule.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.schedulerule.entity.MonthScheduleRule;
import com.ruoyi.project.business.schedulerule.mapper.MonthScheduleRuleMapper;
import com.ruoyi.project.business.schedulerule.request.AddMonthScheduleRuleRequest;
import com.ruoyi.project.business.schedulerule.request.ListMonthScheduleRuleRequest;
import com.ruoyi.project.business.schedulerule.response.MonthScheduleRuleResponse;
import com.ruoyi.project.business.schedulerule.service.IMonthScheduleRuleService;
import com.ruoyi.project.business.utils.BusinessUtil;
import com.ruoyi.project.system.service.ISysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 排班规则Service业务层处理
 * 
 * @author ${author}
 * @date 2024-03-06
 */
@Service
public class MonthScheduleRuleServiceImpl implements IMonthScheduleRuleService {
    @Autowired
    private MonthScheduleRuleMapper monthScheduleRuleMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询排班规则
     * 
     * @return 排班规则
     */
    @Override
    public MonthScheduleRule selectMonthScheduleRuleById(Long id) {
        return monthScheduleRuleMapper.selectMonthScheduleRuleById(id);
    }

    /**
     * 查询排班规则列表
     * 
     * @return 排班规则
     */
    @Override
    public List<MonthScheduleRuleResponse> selectMonthScheduleRuleList(ListMonthScheduleRuleRequest request, Long tenantId) {
        // TODO request to  param
        MonthScheduleRule param = BeanUtils.copy(request, MonthScheduleRule.class);
        param.setTenantId(tenantId);
        List<MonthScheduleRule> entitis = monthScheduleRuleMapper.selectMonthScheduleRuleList(param);
        List<MonthScheduleRuleResponse> resList = BeanUtils.copyList(entitis, MonthScheduleRuleResponse.class);
        BusinessUtil.fillListNames(resList, tenantId);
        return resList;
    }

    /**
     * 新增排班规则
     * 
     * @return 结果
     */
    @Override
    public int insertMonthScheduleRule(AddMonthScheduleRuleRequest request, Long operatorId, Long tenantId) {

        Date yearMonth = request.getYearMonth();
        Integer year = DateUtils.year(yearMonth);
        Integer month = DateUtils.month(yearMonth);

        MonthScheduleRule entity = BeanUtils.copy(request, MonthScheduleRule.class);
        entity.setYear(year);
        entity.setMonth(month);
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setCreator(operatorId);
        entity.setDeleted(Boolean.FALSE);
        entity.setCreateTime(DateUtils.getNowDate());
        return monthScheduleRuleMapper.insertMonthScheduleRule(entity);
    }

    /**
     * 修改排班规则
     * 
     * @return 结果
     */
    @Override
    public int updateMonthScheduleRule(MonthScheduleRule entity, Long operatorId, Long tenantId) {
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setUpdater(operatorId);
        return monthScheduleRuleMapper.updateMonthScheduleRule(entity);
    }

    /**
     * 批量删除排班规则
     * 
     * @return 结果
     */
    @Override
    public int deleteMonthScheduleRuleByIds(Long[] ids, Long operatorId, Long tenantId) {
        return monthScheduleRuleMapper.deleteMonthScheduleRuleByIds(ids);
    }

    /**
     * 删除排班规则信息
     * 
     * @return 结果
     */
    @Override
    public int deleteMonthScheduleRuleById(Long id, Long operatorId, Long tenantId) {
        return monthScheduleRuleMapper.deleteMonthScheduleRuleById(id);
    }

    @Override
    public List<MonthScheduleRule> selectRulesByUserIds(List<Long> userIds, int year, int month, Long tenantId) {
        if(CollectionUtils.isEmpty(userIds)) {
            return new ArrayList<>();
        }
        List<MonthScheduleRule> list = monthScheduleRuleMapper.selectRulesByUserIds(userIds, year, month, tenantId);
        return list;
    }
}
